odoo.define('test_report.test_report_query_action',function (require) {
"use strict";

var core = require('web.core');
var Widget = require('web.Widget');
var Model = require('web.Model');
var QWeb = core.qweb;
var AbstractAction = require('web.AbstractAction');

var TestReportQuery = AbstractAction.extend({
    init: function (parent, context) {
        var self = this;
        this._super(parent);
        if (context.context.active_id) {
            this.report_id = context.context.active_id;
            sessionStorage.setItem("report_id", this.report_id);
        } else {
            this.report_id = parseInt(sessionStorage.getItem("report_id"));
        }
        if (context.context.lines) {
            this.context = context.context;
            sessionStorage.setItem("context", JSON.stringify(this.context));
        }
        else{
            this.context = JSON.parse(sessionStorage.getItem("context"));
        }
    },
    events:{},
    start:function () {
        var self = this;
        var context = self.context;
        var $content = $(QWeb.render("test_report_query", {
                widget:this,
                result : context.result,
                fields : context.fields,
                title : context.title,
                amount_fields : context.amount_fields,
            }));
        self.$el.append($content);

     this.$(".report-test-download").click(function () {
        var model_name = "test.report";
        var active_model = "test.report";
        var report_model = new Model(model_name);
        report_model.call("report_print", [self.report_id])
        .then(function (action) {
            action.context = {
                'active_id': self.report_id,
                'active_model': active_model,
                'active_ids': [self.report_id],
            };
            self.do_action(action);
        });
    });
     this.$(".report-test-refresh").click(function () {
         var model_name = "test.report";
         var active_model = "test.report";
         var report_model = new Model(model_name);
         report_model.call("report_view",[self.report_id])
         .then(function (action) {
             action.context = {
                'active_id': self.report_id,
                'active_model': active_model,
                'active_ids': [self.report_id],
            };
            self.do_action(action);
         });
     });
     this.line_row_click();
    },
    line_row_click:function () {
        var self = this;
        var model_name = "test.report";
        var prod_obj=new Model(model_name);
        var context = prod_obj.context();
        var report_model = new Model(model_name);
        this.$(".test_report_view").on('click', function () {
            report_model.call("link_to_purchase_order", [id, context])
            .then(function (action) {
                self.do_action(action);
            });
        });
    },
});
core.action_registry.add('test_report_query', TestReportQuery);
});